using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace QuotesPlugin
{
    public partial class CheckedListBoxWithImage : UserControl
    {        
        public CheckedListBoxWithImage()
        {
            InitializeComponent();
            this.BackColor = Color.White;

            this.AutoScroll = true;
            this.AutoScrollMinSize = new Size(600, 500);
        }
        
        private bool PointInRectangle(Point pt, Rectangle rect)
        {
            return (pt.X <= rect.Right) && 
                   (pt.X >= rect.Left)  &&
                   (pt.Y >= rect.Top)   && 
                   (pt.Y <= rect.Bottom);
        }
        
        public void Add(IQuotesEntry entry)
        {
            QuotesWidget widget = new QuotesWidget(entry);
            int top = 0;
            foreach (Control control in this.Controls)
            {
                if (control is QuotesWidget)
                    top += control.Height;
            }

            widget.Top = top;            
            this.Controls.Add(widget);

            widget.QuotesChecked += new QuotesWidget.QuotesCheckedHandler(widget_QuotesChecked);
        }

        void widget_QuotesChecked(object sender, QuotesWidget.QuotesEventArgs e)
        {
            if (e.Checked)
            {
                this.SelectionChanged(this, new SelectionChangedEventArgs(SelectionChangedEventArgs.ChangeType.Add));
            }
            else
                this.SelectionChanged(this, new SelectionChangedEventArgs(SelectionChangedEventArgs.ChangeType.Delete));
        }

        public int[] CheckedIndices
        {
            get
            {
                List<int> indices = new List<int>(); 
                for (int i=0; i<this.Controls.Count; i++)
                {
                    if (GetItemChecked(i))
                        indices.Add(i);
                }
                return indices.ToArray();
            }
        }

        public bool GetItemChecked(int idx)
        {
            QuotesWidget widget = this.Controls[idx] as QuotesWidget;
            if (widget != null)
            {
                return widget.Checked;
            }
            return false;
        }

        #region "Events & Delegates"
        public class SelectionChangedEventArgs : EventArgs
        {
            public enum ChangeType
            {
                Add,
                Delete
            }
            public ChangeType Change
            {
                get { return m_change; }
            }
            public SelectionChangedEventArgs(ChangeType ct)
            {
                m_change = ct;
            }
            private ChangeType m_change;
        }
        public delegate void SelectionChangedHandler(object sender, SelectionChangedEventArgs e);
        public event SelectionChangedHandler SelectionChanged;
        #endregion
    }
}
